home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue36 / Clinic / LottryU2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-04-14  |  2.9 KB  |  124 lines

  1. unit LottryU2;
  2.  
  3. interface
  4.  
  5. uses
  6.   WinProcs, WinTypes, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TfrmLottery = class(TForm)
  11.     lblNo1: TLabel;
  12.     lblNo2: TLabel;
  13.     lblNo3: TLabel;
  14.     lblNo4: TLabel;
  15.     lblNo5: TLabel;
  16.     lblNo6: TLabel;
  17.     pnlDisplay: TPanel;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormDestroy(Sender: TObject);
  20.     procedure GenericClick(Sender: TObject);
  21.   private
  22.     List: TList;
  23.   end;
  24.  
  25. var
  26.   frmLottery: TfrmLottery;
  27.  
  28. implementation
  29.  
  30. {$R *.DFM}
  31.  
  32. {$ifndef Win32}
  33. procedure Sleep(MSec: Integer);
  34. var
  35.   OldTime: TDateTime;
  36. begin
  37.   OldTime := Now;
  38.   repeat until Now >= OldTime + MSec / MSecsPerDay
  39. end;
  40. {$endif}
  41.  
  42. procedure AnimateLabel(Lbl: TLabel; Loops, Delay: Integer);
  43. var
  44.   Loop: Integer;
  45. const
  46.   Chars: String = '/-\I';
  47. begin
  48.   for Loop := 1 to Loops do
  49.   begin
  50.     Lbl.Caption := Chars[Loop mod Length(Chars) + 1];
  51.     { Post label's parent a repaint message }
  52.     Lbl.Parent.Invalidate;
  53.     { Allow all posted messages to be processed }
  54.     Application.ProcessMessages;
  55.     { Let user bail out if bored }
  56.     if Application.Terminated then Break;
  57.     Sleep(Delay)
  58.   end
  59. end;
  60.  
  61. procedure SortNumbers(Lbls: array of TLabel);
  62. var
  63.   Loop, Loop2: Integer;
  64.   Tmp: String;
  65. begin
  66.   for Loop := Low(Lbls) to High(Lbls) do
  67.     for Loop2 := Loop + 1 to High(Lbls) do
  68.       if StrToInt(Lbls[Loop].Caption) > StrToInt(Lbls[Loop2].Caption) then
  69.       begin
  70.         Tmp := Lbls[Loop].Caption;
  71.         Lbls[Loop].Caption := Lbls[Loop2].Caption;
  72.         Lbls[Loop2].Caption := Tmp
  73.       end
  74. end;
  75.  
  76. procedure TfrmLottery.FormCreate(Sender: TObject);
  77. begin
  78.   Randomize;
  79.   List := TList.Create
  80. end;
  81.  
  82. procedure TfrmLottery.FormDestroy(Sender: TObject);
  83. begin
  84.   List.Free;
  85.   List := nil
  86. end;
  87.  
  88. procedure TfrmLottery.GenericClick(Sender: TObject);
  89. var
  90.   Loop, CharLoop, Index: Integer;
  91. const
  92.   MaxNum = 49;
  93.   { Re-entry protection flag }
  94.   InHandler: Boolean = False;
  95. begin
  96.   if not InHandler then
  97.   begin
  98.     InHandler := True;
  99.     { Empty the list }
  100.     List.Clear;
  101.     { Fill the list with 49 numbers }
  102.     for Loop := 1 to MaxNum do
  103.       List.Add(Pointer(Loop));
  104.     { Loop for each number sought }
  105.     for Loop := 1 to 6 do
  106.     begin
  107.       { Choose one of the remaining numbers in the list }
  108.       Index := Random(List.Count);
  109.       { Do "pretty" animation }
  110.       AnimateLabel(FindComponent('lblNo' + IntToStr(Loop)) as TLabel, 15, 50);
  111.       { Write it in the appropriate label }
  112.       TLabel((FindComponent('lblNo' + IntToStr(Loop)))).Caption :=
  113.         IntToStr(Longint(List[Index]));
  114.       { Remove the number from the list, so it won't be picked again }
  115.       List.Delete(Index);
  116.     end;
  117.     ShowMessage('Press OK to sort the numbers into numerical order');
  118.     SortNumbers([lblNo1, lblNo2, lblNo3, lblNo4, lblNo5, lblNo6]);
  119.     InHandler := False
  120.   end;
  121. end;
  122.  
  123. end.
  124.